import java.util.ArrayList;
import javax.swing.*;

/**
 * This class defines an HTML table-row (tr) element,
 * which represents as individual row in the table, and
 * a collection of (one or more) table-data (td) elements.
 * 
 * @author Dennis Mitchell 
 * @version 1.001 09/09/2010
 */
public class HtmlTableRow 
{

    // the cells (table-data elements) that the row holds
    private ArrayList<HtmlTableCell> cells;

    
    //default size for JFrame
    private static final int FRAME_WIDTH=500;
    private static final int FRAME_HEIGHT=500;
        
    
    /**
     * Constructor for the {@code HtmlTableRow} class. Initializes the
     * ArrayList of cells that the row holds.
     */
    public HtmlTableRow()
    {
        cells = new ArrayList<HtmlTableCell>();  
    }
    
    /**
     * Adds an individual cell (table-data element)
     * to the row
     */
    public void addTableCell(HtmlTableCell cell)
    {
        cells.add(cell);
    }
    
    /**
     * Removes all cells (table-data elements) from
     * the row
     */
    public void clearRow()
    {
        cells.clear();
    }
    
    /**
     * Gets the HTML for the row (table-data element), 
     * including all cells and associated inline style declarations.
     * @return String holding the tr element and td elements
     */    
    public String getHtml()
    {
        //instantiate new StringBuilder        
        StringBuilder html = new StringBuilder();
        
        //create start element for table row
        html.append("<tr>");
                        
        //for each cell in the row, append the html string
        for (HtmlTableCell c : cells)
            html.append(c.getHtml());

        //append end element for table row
        html.append("</tr>");
        
        //return the StringBuilder's String
        return html.toString();
        
    }
    
    /**
     *  Wraps the row in a table and displays the row
     *  in a JLabel within a JFrame
     */
    public void showInJFrame()
    {
            //instantiate a new JFrame with caption for month and year
            JFrame frame = new JFrame("Table Row");
            
            //build the HTML for the table-row element
            String html = "<html><body><table>" + getHtml() + "</table></body></html>";
            
            
            //instantiate a new JLabel, whose contents are the HTML calendar,
            //and center the calendar JLabel
            JLabel htmlTable = new JLabel(html, SwingConstants.CENTER);
            
            //add the calendar JLabel to the JFrame
            frame.getContentPane().add(htmlTable);
            
            //set the JFrame's size
            frame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
            
            //make the JFrame visible
            frame.setVisible(true);        
        
        
    }       
    
}
